library(ggplot2)
library(effects)
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}
rm(list=c(ls()))
load("final/study3_1.RData")

dataTear$pid <- droplevels(dataTear$pid)
sink("final/s3_race.tex")
latex(  tabular(  (Race=race) ~  (Percent("col")+ 1)    ,data=dataTear        ))
sink()

sink("final/s3_gender.tex")
latex(  tabular(  (Gender=gender) ~  (Percent("col")+ 1)    ,data=dataTear        ))
sink()

sink("final/s3_edu.tex")
latex(  tabular(  (Education=education) ~  (Percent("col")+ 1)    ,data=dataTear        ))
sink()

sink("final/s3_pid.tex")
latex(  tabular(  (PartyID=pid) ~  (Percent("col")+ 1)    ,data=dataTear        ))
sink()
#\caption{Support for Punishing Protesters} \label{f:result311}
#\scalebox{0.7}{\includegraphics{plots/punishAgree.pdf}}

model<- lm(agreedisagee~affectivepolarization*Condition+gender+race+income+education+pid,subset(dataTear))
arm::display(model)
eff<-effect(model,term="affectivepolarization*Condition",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

d<-ggplot(data=dataeff, aes(x=affectivepolarization, y=fit)) + geom_line() + geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ scale_y_continuous(limits=c(0, 1))+ scale_x_continuous(limits=c(0, 1)) + xlab("Affective Polarization of the Participant")+ylab("Agreement with Protestor Punishment") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d + facet_grid(. ~ Condition)   +theme(panel.margin = unit(1, "lines")) + theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())                       
d
ggsave(filename="plots/punishAgree.pdf", plot=d,width=9,height=4)

#\caption{Logged Amount of the Suggested Protester Fine} \label{f:result312}
#\scalebox{0.7}{\includegraphics{plots/punishAmount.pdf}}
modelout <- lm(log1p(fineamount)~affectivepolarization*Condition+gender+race+income+education+pid,subset(dataTear))
eff<-effect(modelout,term="affectivepolarization*Condition",as.table=T,default.levels=100,typical = "median",xlevels=list(affectivepolarization=seq(0, 1, by=.01)))
dataeff<-as.data.frame(eff)

d<-ggplot(data=dataeff, aes(x=affectivepolarization, y=fit)) + geom_line() + geom_ribbon(aes(ymin=lower, ymax=upper),alpha=0.2,linetype=0)+ scale_y_continuous(limits=c(0, 12))+ scale_x_continuous(limits=c(0, 1)) + xlab("Affective Polarization of the Participant")+ylab("Log of Fine Amount") + theme_bw() + scale_colour_manual(values=c("#4d4d4d","#4d4d4d")) 
d<-d + facet_grid(. ~ Condition)   +theme(panel.margin = unit(1, "lines")) + theme(panel.grid.major = element_line(colour = "white"),panel.grid.minor = element_line(colour = "white"),axis.title.x = element_text(vjust=-0.5)) +theme(legend.title=element_blank())                       
d
ggsave(filename="final/punishAmount.pdf", plot=d,width=9,height=4)

modelin <- lm(agreedisagee~affectivepolarization,subset(dataTear,Condition=='Co-Partisans'))
modelin1 <- lm(agreedisagee~affectivepolarization+gender+race+income+education+pid,subset(dataTear,Condition=='Co-Partisans'))
modelout <- lm(agreedisagee~affectivepolarization,subset(dataTear,Condition=='Opposing Partisans'))
modelout1 <- lm(agreedisagee~affectivepolarization+gender+race+income+education+pid,subset(dataTear,Condition=='Opposing Partisans'))

stargazer(modelin,modelin1,modelout,modelout1,covariate.labels=c("Affective Polarization","Female","White","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Republican","Intercept"),out = "final/three_1a_appendix.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and support for police action",dep.var.labels.include = F,model.numbers = F,model.names = F,column.labels = c("In-Party","In-Party","Out-Party","Out-Party"),star.cutoffs = c(.05,.01,.001),no.space = T)

stargazer(modelin,modelin1,modelout,modelout1,ci = T)


### By party
modelin1 <- lm(agreedisagee~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Co-Partisans' & pid=='Republican'))
modelin2 <- lm(agreedisagee~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Co-Partisans' & pid=='Democrat'))


modelout1 <- lm(agreedisagee~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Opposing Partisans' & pid=='Republican'))
modelout2 <- lm(agreedisagee~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Opposing Partisans' & pid=='Democrat'))


stargazer(modelin1,modelout1,modelin2,modelout2,covariate.labels = c("Affective Polarization","Female","White","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Intercept"),out = "final/three_1a_appendix_byparty.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and support for police action",dep.var.labels.include = T,model.numbers = F,model.names = F,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),star.cutoffs = c(.05,.01,.001),no.space = T,multicolumn = T,dep.var.labels = c("Republican","Republican","Democrat","Democrat"))



## Fine
## Fine
modelin1 <- lm(log1p(fineamount)~affectivepolarization,subset(dataTear,Condition=='Co-Partisans' ))
modelin2 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education+pid,subset(dataTear,Condition=='Co-Partisans' ))


modelout1 <- lm(log1p(fineamount)~affectivepolarization,subset(dataTear,Condition=='Opposing Partisans' ))
modelout2 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education+pid,subset(dataTear,Condition=='Opposing Partisans' ))


stargazer(modelin1,modelin2,modelout1,modelout2,covariate.labels = c("Affective Polarization","Female","White","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Republican","Intercept"),out = "final/three_1b_appendix.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and amount of fine",dep.var.labels = c("Republican","Republican","Democrat","Democrat"),model.numbers = F,model.names = F,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),star.cutoffs = c(.05,.01,.001),no.space = T,multicolumn = T)


modelin1 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Co-Partisans' & pid=='Republican'))
modelin2 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Co-Partisans' & pid=='Democrat'))


modelout1 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Opposing Partisans' & pid=='Republican'))
modelout2 <- lm(log1p(fineamount)~affectivepolarization+gender+race+income+education,subset(dataTear,Condition=='Opposing Partisans' & pid=='Democrat'))

stargazer(modelin1,modelin2,modelout1,modelout2,covariate.labels = c("Affective Polarization","Female","White","Income: 30-59k","Income: 60-79k","Income: 80k+","Education: College+","Education: Some College","Intercept"),out = "final/three_1b_appendix_byparty.tex",dep.var.caption = "",keep.stat = c("n","adj.rsq"),title="Affective polarization and amount of fine",dep.var.labels = c("Republican","Republican","Democrat","Democrat"),model.numbers = F,model.names = F,column.labels = c("In-Party","Out-Party","In-Party","Out-Party"),star.cutoffs = c(.05,.01,.001),no.space = T,multicolumn = T)
